/*
 * SoftMenu.java
 * Created on July 25, 2007, 11:42 AM
 */

package view;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import utils.Util;

/**
 * 
 * @author erol
 */
public class SoftMenuButton extends Component {        
    
    public static final int LEFT = 0; // secenekler
    public static final int RIGHT = 1;        
    
    private String label;        
    private Color lineColor;
    
    public static int softButtonHeight;
    
    private boolean active;
    
    /**
     *  LEFT or RIGHT 
     */
    private int position;
        
    private SoftMenu softMenu;
    private Functor defaultCommand;
    
    
    public SoftMenuButton(int position) {        
        this(position, 0, 0, "");
    }
    
    public SoftMenuButton(int position, int width, int height, String label) {        
        
        super(width, height);        
        this.position = position;
        this.label = label;
        SoftMenuButton.softButtonHeight = height;
    }
    
    public SoftMenuButton(int position, Image image) {        
        this(position, image.getWidth(), image.getHeight(), null);        
        this.bgImage = image;
    }
    
    protected void init() {
        lineColor = getFgColor();
    }

    public void paint(Graphics g) {                
        
        // paint visible line
        if(bgImage == null){
            
                // paint label text
            GuiUtils.setColor(g, getFgColor());
            g.drawString(label, get_x_content(), get_y_content(), Graphics.TOP|Graphics.LEFT);
            
            GuiUtils.setColor(g, lineColor);
            g.drawRect(get_x_content(), get_y_content(), getWholeWidth(), getWholeHeight());
        }
    }        

    public void initPaint() {
        
        y = container.getHeight() - height;
        
        if(LEFT == position){
            x = 0;            
        } else if(RIGHT == position) {
            x = container.getWidth() - width;
        }        
    }

    public void setHeight(int height){
        super.setHeight(height);
        SoftMenuButton.softButtonHeight = height;
    }
    
    public void doCtrl(){                
        
        if(softMenu != null){
            
            active = true;
            
            if(softMenu.isFocused()){
                softMenu.setVisible(false);                
                softMenu.doFire();                
            } else {           
                softMenu.setVisible(true);
                container.focusOnComponent(softMenu);
            }
            
        } else if(defaultCommand != null){
            
            defaultCommand.doFunc();
        }
    }
    
    public void undoCtrl(){
        if(softMenu != null){
            softMenu.setVisible(false);
        }
        container.passivateSoftButtons(); 
        container.focusLast();
    }           
    
    //--------------------------------------------------------------------
    
    public void setMenuElements(SoftMenu softMenu) {
        
        this.softMenu = softMenu;        
        container.add(softMenu);
        softMenu.setupList();
        softMenu.setSoftButtonType((byte) position);
    }
    
    //--------------------------------------------------------------------
    
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public Functor getDefaultCommand() {
        return defaultCommand;
    }

    public void setDefaultCommand(Functor defaultCommand) {
        this.defaultCommand = defaultCommand;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    protected void setHighLevelProperty(int property, String value) {
        
        if(Fields.LABEL == property){
            
            label = value;
            
        } else if(Fields.LINE_COLOR == property){
            
            lineColor = GuiUtils.getColor(value);
            
        } else if(Fields.SOFT_BUTTON_HEIGHT == property){
            
            softButtonHeight = Integer.parseInt(value);
            
        } else if(Fields.ACTIVE == property){
            
            active = Util.booleanValue(value);
            
        } else if(Fields.POSITION == property){
            
            position = Integer.parseInt(value);
            
        } else if(Fields.DEFAULT_COMMAND == property){
            
            
            
        } else if(Fields.SOFT_MENU == property){
            
            
            
        }  
    }        
        
}
